home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / libkb100.zip / LIBKB-1.00 / UTIL / 8.C < prev    next >
C/C++ Source or Header  |  1996-07-23  |  3KB  |  104 lines

  1. /* 8.c -- Linux emergency keyboard restorer
  2.  * Copyright (C) 1996 Markus F.X.J. Oberhumer
  3.  * For conditions of distribution and use, see copyright notice in kb.h 
  4.  */
  5.  
  6.  
  7. /***********************************************************************
  8. // Put this program into your path. 
  9. // If a program crashes and the keyboard is still in
  10. // raw or mediumraw mode, do the following:
  11. // 
  12. // Press the left Alt key (scancode 56 -> '8') and then
  13. // the equal key (scancode 13 -> '\n').  This program will
  14. // be started and the keyboard should be useable again.
  15. // 
  16. // You can modify the default values below to suit your needs,
  17. // but probably not.
  18. ************************************************************************/
  19.  
  20. #if !defined(__linux__)
  21. #  error this program is for Linux only
  22. #endif
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <termios.h>
  30. #include <sys/types.h>
  31. #include <sys/ioctl.h>
  32. #include <linux/kd.h>
  33.  
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.     int fd;
  38.     int mode;
  39.     struct termios t;
  40.     int restore = 1;
  41.  
  42.     /* option -d: display the current values */
  43.     if (argc == 2 && strncmp(argv[1],"-d",2) == 0)
  44.         restore = 0;
  45.  
  46.     fd = open("/dev/console", O_RDONLY);
  47.     if (fd < 0)
  48.     {
  49.         perror("cannot open /dev/console");
  50.         return 1;
  51.     }
  52.  
  53.     if (ioctl(fd, KDGKBMODE, &mode) != 0)
  54.         perror("ioctl KDGKBMODE");
  55.     printf("KDGKBMODE: kbmode=%d\n", mode);
  56.  
  57. /* that's the most important thing */
  58.     if (restore)
  59.     {
  60.         mode = K_XLATE;
  61.         if (ioctl(fd, KDSKBMODE, mode) != 0)
  62.             perror("ioctl KDSKBMODE");
  63.         if (ioctl(fd, KDGKBMODE, &mode) != 0)
  64.             perror("ioctl KDGKBMODE");
  65.         printf("KDSKBMODE: kbmode=%d\n", mode);
  66.     }
  67.  
  68.     /* set termios */
  69.     t.c_iflag = t.c_oflag = t.c_lflag = 0;
  70.     if (tcgetattr(fd, &t) != 0)
  71.         perror("tcgetattr");
  72.     printf("tcgetattr: iflag=0x%04lx oflag=0x%04lx lflag=0x%04lx %d %d\n", 
  73.         t.c_iflag, t.c_oflag, t.c_lflag, t.c_cc[VMIN], t.c_cc[VTIME]);
  74.     t.c_iflag |= (IXOFF | IXON | ICRNL);
  75.     t.c_oflag |= (OPOST | ONLCR);
  76.     t.c_lflag |= (ISIG | ICANON | ECHO);
  77.     t.c_lflag |= (ECHOE | ECHOK | ECHOKE);
  78.     t.c_cc[VMIN] = 1;
  79.     t.c_cc[VTIME] = 0;
  80.     if (restore)
  81.     {
  82.         if (tcsetattr(fd, TCSANOW, &t) != 0)
  83.             perror("tcsetattr");
  84.         if (tcgetattr(fd, &t) != 0)
  85.             perror("tcgetattr");
  86.         printf("tcgetattr: iflag=0x%04lx oflag=0x%04lx lflag=0x%04lx %d %d\n", 
  87.             t.c_iflag, t.c_oflag, t.c_lflag, t.c_cc[VMIN], t.c_cc[VTIME]);
  88.     }
  89.  
  90.     /* a good idea to add this here */
  91. #if 1
  92.     /* note: you must run 'savetextmode' before */
  93.     if (restore)
  94.         system("textmode");
  95. #endif
  96.     
  97.     return 0;
  98. }
  99.  
  100.  
  101. /*
  102. vi:ts=4
  103. */
  104.